import {observable, action, /*toJS*/} from 'mobx';
import CKRichEditorStore from './CKRichEditorStore';
import { Reply } from 'weaver-mobile-page';
export default class RichTextStore{
  @observable richEditorStore = new CKRichEditorStore({initEndCallback: this.registerRichEditor});
  @observable replyStore;
  replyRef = null;
  constructor(params) {
    this.replyStore = new Reply.Store({getAttachList: params.getAttachList });
  }
  
  @action
  insertHtml = (...args) => {
    return this.richEditorStore.insertHtml(...args);
  }
  setReplyRef = (ref) => {
    this.replyRef = ref;
  }
  registerRichEditor = () => {
    const {editor} = this.richEditorStore;
    editor.on('focus', e => { // onFocus时关闭reply组件
      this.replyRef && this.replyRef.setVisible(false);
    });
  }
  
  //  以下都是调用insertHTML方法，插入需要的html数据 (具体要什么样的html自定义)
  onEmojiChange = (emoji) => this.insertHtml(`<span>${emoji}</span>`)
  
  onImgChange = (datas) => {
    datas.forEach(v => {
      this.insertHtml(`<img src=${v.imgSrc} class='mobileBlogImgInnerTemp' />`);
    })
  }
  
  onAtChange = (datas) => {
    datas.forEach(v => {
      this.insertHtml(`<a id=${v.id}>${v.name}</a>&nbsp;&nbsp;&nbsp;`)
    });
  }
  
  onBrowserChange = (key, datas) => {
    datas.forEach(v => {
      this.insertHtml(`<a href="javascript:void(0)" style="text-decoration:underline !important;" linkid="${v.id}" linktype="${key}" onclick="try{return openAppLink(this,${v.id});}catch(e){}">${v.name}</a>&nbsp;&nbsp;&nbsp;`);
    });
  }
  
  onUploadChange = (key, datas) => {
    datas.forEach(v => {
      this.insertHtml(`<a href="javascript:void(0)" style="text-decoration:underline !important;" linkid="${v.id}" onclick="try{opendoc1('${v.id}',this);return false;}catch(e){}">${v.name}</a>&nbsp;`);
      this.insertHtml(`<a href="javascript:void(0)" style="text-decoration:underline !important;" linkid="${v.id}" onclick="try{downloads('${v.id}',this,'${v.name}');return false;}catch(e){}">下载</a>&nbsp;&nbsp;&nbsp;`);
    });
  }
  
  onSpeechChange = (datas) => {
    this.insertHtml(`<span>${datas}</span>`)
  }
  
  getData = () => {
    let richText = this.richEditorStore.getData();
    let datas = this.replyStore.getDatas();
    return {
      ...datas,
      richText,
    }
  }
  
  setRichText = (v) => {
    this.richEditorStore.setData(v);
  }
  
  clear = () => {
    this.richEditorStore.clear();
    this.replyStore.doClear();
  }
  
}